package org.example.forum.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.config.AppConfig;
import org.example.forum.model.Article;
import org.example.forum.model.ArticleReply;
import org.example.forum.model.User;
import org.example.forum.services.IArticleReplyService;
import org.example.forum.services.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    IArticleReplyService articleReplyService;

    @Autowired
    IArticleService articleService;

    @RequestMapping("/create")
    public AppResult create (HttpServletRequest request,
                             @ApiParam("帖子id")@RequestParam("articleId")@NonNull Long articleId,
                             @ApiParam("回复内容")@RequestParam("content")@NonNull String content) {
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户状态
        if(user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子详情
        Article article = articleService.selectById(articleId);
        //校验帖子状态
        if (article == null || article.getDeleteState() == 1) {
            //返回错误结果
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            //返回错误结果
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构造回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        //写入回复
        articleReplyService.create(articleReply);
        //返回结果
        return AppResult.success();
    }

    @ApiOperation("获取回复列表")
    @RequestMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (
            @ApiParam("帖子id")@RequestParam("articleId")@NonNull Long articleId) {
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            //返回错误结果
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        //返回结果
        return AppResult.success(result);
    }
}
